<template>
  <el-card>
    <PageTools :total="total">
      <template #left>
        <el-form ref="form" :model="queryData" label-width="80px" inline>
          <el-form-item label="标签名称">
            <el-input v-model="queryData.tags" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="城市">
            <el-select v-model="queryData.province" placeholder="请选择">
              <el-option
                v-for="(item, index) in city"
                :key="index"
                :value="item"
                :label="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="地区">
            <el-select v-model="queryData.city" placeholder="请选择">
              <el-option
                v-for="(item, index) in area"
                :key="index"
                :value="item"
                :label="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="企业简称">
            <el-input v-model="queryData.shortName" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="queryData.state" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button plain type="primary" @click="enSubmit">清除</el-button>
            <el-button type="primary" @click="initData">搜索</el-button>
            <el-button type="success" @click="addHandle">新增用户</el-button>
          </el-form-item>
        </el-form>
      </template>
    </PageTools>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" />
      <el-table-column prop="number" label="企业编号" />
      <el-table-column prop="shortName" label="企业简称" />
      <el-table-column prop="tags" label="标签" />
      <el-table-column prop="creatorID" label="创建者" />
      <el-table-column prop="addDate" label="创建日期" />
      <el-table-column prop="remarks" label="备注" />
      <el-table-column prop="state" label="状态">
        <template slot-scope="{ row }">
          <el-tag v-if="row.state" type="success">启用</el-tag>
          <el-tag v-else type="warning">禁止</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            circle
            plain
            type="primary"
            icon="el-icon-edit"
            @click="editHandle(row.id)"
          />
          <el-button
            circle
            plain
            :type="`${Boolean(row.state) ? 'warning' : 'success'}`"
            :icon="`el-icon-${Boolean(row.state) ? 'close' : 'check'}`"
            @click="changeState(row)"
          />
          <el-button
            circle
            plain
            type="danger"
            icon="el-icon-delete"
            @click="delHandle(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row type="flex" justify="end">
      <el-pagination
        background
        :current-page="queryData.page"
        :page-sizes="[5, 12, 15, 20]"
        :page-size="5"
        :total="total"
        layout="prev, pager, next, sizes, total, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <com-dialog ref="comdialog" @update="initData" />
  </el-card>
</template>

<script>
import PageTools from '@/components/PageTools'
import { getCompanys, editCompanyState, delCompany } from '@/api/company'
import chooseCity from '@/mixins/chooseCity'
import ComDialog from './components/comDialog.vue'
export default {
  components: {
    PageTools,
    ComDialog
  },
  mixins: [chooseCity],
  data() {
    return {
      total: 0,
      list: [],
      queryData: {
        page: 1,
        pagesize: 5
      },
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 }
      ]
    }
  },
  created() {
    this.initData()
  },
  methods: {
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.initData()
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    async initData() {
      const { counts, items } = await getCompanys(this.queryData)
      this.total = counts
      items.map((item) => {
        item.addDate = new Date(item.addDate).toLocaleString()
        return item
      })
      this.list = items
    },
    enSubmit() {
      this.queryData = {
        page: 1,
        pagesize: 5
      }
      this.initData()
    },
    addHandle() {
      this.$refs.comdialog.dialogVisible = true
    },
    editHandle(id) {
      this.$refs.comdialog.dialogVisible = true
      this.$refs.comdialog.getCompany(id)
    },
    async changeState(val) {
      await this.$confirm('确定修改状态吗')
      await editCompanyState({ id: val.id, state: Number(!val.state) })
      this.$message.success('状态修改成功')
      this.initData()
    },
    async delHandle(id) {
      await this.$confirm('确定删除吗')
      await delCompany(id)
      this.$message.success('删除成功')
      this.initData()
    }
  }
}
</script>

<style scope>
.el-input {
  width: 300px;
}
</style>
